import { createRouter, createWebHistory } from 'vue-router'
import Home from './views/Home.vue'
import Help from './views/help.vue'
import Welcome from './views/Welcome.vue'
import Single from './views/single.vue'
import Double from './views/double.vue'
import Multi from './views/multi.vue'
import Notice from './views/Notice.vue'
import Bookdetail from './views/bookdetail.vue'
import show from './views/show.vue'
import personalpage from './views/personalpage.vue'
import myInf from './views/myInf.vue'
import affairOrg from './views/affairOrg.vue'
import myBorrow from './views/myBorrow.vue'
import payFor from './views/payFor.vue'
import placeAppointed from './views/placeAppointed.vue'
import Notice2 from './views/Notice2.vue'
import zhuye from './views/zhuye.vue'
import Siping from './views/bookDistribute/Siping.vue'
import Jiading from './views/bookDistribute/Jiading.vue'
import bookCheck from './views/bookDistribute/bookCheck.vue'
import login from './views/login.vue'
import register from './views/register.vue'

import manager from './views/Manager.vue'
import M_broken from './views/Manager_Broken.vue'
import M_book from './views/Manager_Book.vue'
import M_notice from './views/Manager_Notice.vue'

import loginAndRegister from './views/loginAndRegister.vue'


// import loginAndRegister from './views/loginAndRegister.vue'
import postSent from './views/post_sent.vue'
import postDetail from './views/postdetail.vue'
import forum from './views/forum.vue'

import homepage from './views/HomePage.vue'

const routes = [
  // {
  //   path: '/home',
  //   component: Home,
  //   redirect: '/welcome',
  //   children: [
  //     { path: '/help', component: Help },
  //     { path: '/welcome', component: Welcome },
  //     { path: '/single', component: Single },
  //     { path: '/double', component: Double },
  //     { path: '/multi', component: Multi },

  //   ]
  // },

  {
    path: '/',
    redirect: '/show',
  },


  {
    path: '/show',
    component: show,
  },


  {
    path: '/HomePage',
    component: homepage,
  },


  {
    path: '/login',
    component: login,
  },

  {
    path: '/register',
    component: register,
  },

  {
    path: '/loginAndRegister',
    component: loginAndRegister,
  },


  
  {
    path: '/zhuye',
    component: zhuye,
    redirect: '/book',
    children: [
      { path: '/book', component: Bookdetail },
      { path: '/forum', component: forum },
      {
        path: '/postsent',
        component: postSent,
      },
      {
        path: '/Siping',
        component: Siping
      },
      {
        path: '/Jiading',
        component: Jiading
      },
      {
        path: '/bookCheck',
        component: bookCheck
      },
      {
        path: '/postdetail',
        component: postDetail,
      },
      {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [
          { path: '/help', component: Help },
          { path: '/welcome', component: Welcome },
          { path: '/single', component: Single },
          { path: '/double', component: Double },
          { path: '/multi', component: Multi },
        ]
      },
      {
        path: '/personalpage',
        component: personalpage,
        redirect: '/myInf',
        children: [
          { path: '/myInf', component: myInf },
          { path: '/affairOrg', component: affairOrg },
          { path: '/myBorrow', component: myBorrow },
          { path: '/payFor', component: payFor },
          { path: '/placeAppointed', component: placeAppointed },
          { path: '/notice2', component: Notice2 }
        ]
      },
      {
        path: '/notice',
        component: Notice,
      },
      {
        path: '/Manager',
        component: manager,
        redirect: '/Broken',
        children: [
          { path: '/Broken', component: M_broken },
          { path: '/BookManage', component: M_book },
          { path: '/NoticeManage', component: M_notice },
        ]
      }
    ]
  },




]

const router = createRouter({
  routes,
  history: createWebHistory(process.env.BASE_URL),
  scrollBehavior() {
    return { top: 0 }
  }
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 从哪个路径跳转
  // next 放行或跳转

  // if (from.path === '/show') next()
  if (to.path === '/login' || to.path === '/register' || to.path === '/show' || to.path === '/loginAndRegister') return next();

  // 获取userid
  const useridStr = window.sessionStorage.getItem('userid')
  if (!useridStr) return next('/login')
  next()
})

export default router
